/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entities.Uzivatel;
import exceptions.UserNotFoundException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Michal Vachler
 */
@Stateless()
public class UzivatelFacade extends AbstractFacade<Uzivatel> implements IUzivatelFacade{
    @PersistenceContext(unitName = "SimpleEE6AppPU")
    private EntityManager em;

    //prepisane z protected na public /lachky
    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public UzivatelFacade() {
        super(Uzivatel.class);
    }
    
    /**
     * Vrátí uživatele podle parametru "login", jinak vyhodi UserNotFoundException
     * @param login
     * @return Uzivatel
     * @throws UserNotFoundException
     */
    public Uzivatel findUserByLogin(String login) throws UserNotFoundException {
        Uzivatel nalezen =(Uzivatel)em.createQuery("SELECT u FROM Uzivatel AS u WHERE u.login = :login") 
                .setParameter("login", login)
                .getSingleResult();
         if(nalezen == null){
             throw new UserNotFoundException();
         }
         else{
             return nalezen;
         }
    }
    
    /**
     * Ověří, zda zadaný Uzivatel s loginem existuje v DB
     * @param login
     * @return boolean
     */
    public boolean kontrolaExistujicihoUzivatele(String login){
       Uzivatel nalezen;
       nalezen =(Uzivatel)em.createQuery("SELECT u FROM UZIVATEL u WHERE u.LOGIN LIKE :login") 
                .setParameter("login", login)
                .getSingleResult();
       return nalezen != null;
    }
    
}
